﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TreadConcurrentTest1;
using System.Threading.Tasks;
using System.Threading;


namespace TestProject
{
    public class StartMain
    {
        static int count = 0;
        //public static void Main(string[] args)
        //{
        //    //string[] GuidArr = new string[9000];
        //    //for (int i = 0; i < GuidArr.Length; i++)
        //    //{
        //    //    GuidArr[i] = Guid.NewGuid().ToString();
        //    //}
        //    ////for
        //    int keyLength = 1000;
        //    DictionaryTest theDic = new DictionaryTest();
        //    Thread[] tArray = new Thread[keyLength];
        //    ManualResetEvent manualHandle = new ManualResetEvent(false);
        //    try
        //    {
        //        for (int i = 0; i < keyLength; i++)
        //        {
                    
        //            new Thread(() =>
        //            {
        //                //while (true)
        //                //{
        //                    //if(i > 3000 || i < 2000)
        //                    //manualHandle.WaitOne();
        //                    string key = "";
        //                    key = theDic.AddDic();
        //                    new Thread(() =>
        //                    {
        //                        theDic.RemoveDic(key);
        //                    }
        //                    ).Start();
        //                    //Console.WriteLine("Thread={0}, {2}: key={1}, AddSuccess", Thread.CurrentThread.ManagedThreadId, key, Interlocked.Increment(ref count));
        //                    //break;
        //                //}
        //            }
        //            ).Start();
        //            Thread.Sleep(10);
        //           /// tArray[i] = t;
        //        }

        //        //new Thread(() =>
        //        //{
        //        //    for (int i = 0; i < keyLength; i++)
        //        //    {
        //        //        tArray[i].Start();
        //        //    }
        //        //}).Start();
        //        //Thread.Sleep(5000);

        //        lock (theDic.aa)//.testDictionary)
        //        {
        //            Thread.Sleep(5000);
        //            //manualHandle.Set();
        //            Thread.Sleep(5000);
        //            Console.WriteLine("#####################################################################################################");
        //            Console.WriteLine("#####################################################################################################");
        //            Console.WriteLine("#####################################################################################################");
        //            Console.WriteLine("#####################################################################################################");
        //            //theDic.testDictionary
        //        }
        //        //Parallel.ForEach(tArray, (a) =>
        //        //{
        //        //    a.Start();
        //        //}
        //        //);

                
        //    }
        //    catch (Exception ex)
        //    {
        //    }


        //    Console.ReadLine();

        //}
        //new Thread(() =>
        //                Parallel.ForEach(GuidArr, (a) =>
        //                        {
        //                            theDic.AddDic(a);
        //                            Thread.Sleep(5000);
        //                            //Console.WriteLine("Thread={0}, key={1}, AddSuccess", Thread.CurrentThread.ManagedThreadId, a);
        //                        }
        //                        )).Start();
        //        Thread.Sleep(1000);

        //        new Thread(() =>
        //        Parallel.ForEach(GuidArr, (a) =>
        //        {
        //            theDic.Remove(a);
        //            Thread.Sleep(5000);
        //            //Console.WriteLine("Thread={0}, key={1}, AddSuccess", Thread.CurrentThread.ManagedThreadId, a);
        //        }
        //       )).Start();
        public static int successNumber;
        public static int failNumber;

        //public static void Main(string[] args)
        //{
        //    int count = 1000;
        //    //Thread[] tArray = new Thread[count];
        //    for (int i = 0; i < count; i++)
        //    {
        //        DoRequest();
        //    }

        //    Console.WriteLine("SuccessNumber={0}", successNumber);
        //    Console.WriteLine("FailNumber={0}", failNumber);
        //    Console.ReadLine();
        //}

        public static string Request = @"<Request>
  <Header Culture=""en"" UserID=""110117"" RequestID=""6ef9bc0e-3cfa-4423-adb1-7cba9cf2c315"" RequestType=""Hotel.InterfaceSwitch.GetHotelAvailRequest"" ClientIP=""172.16.189.98"" AsyncRequest=""false"" Timeout=""0"" MessagePriority=""3"" AssemblyVersion=""2.7.0.2"" RequestBodySize=""0"" SerializeMode=""Xml"" RouteStep=""1"" Environment=""fat2"" UseMemoryQ=""false"" />
  <GetHotelAvailRequest>
    <HotelAvalID>28264</HotelAvalID>
    <Hotel>353933</Hotel>
    <Room>1223658</Room>
    <Arrival>2014-06-06</Arrival>
    <Departure>2014-06-07</Departure>
    <RoomQuantity>1</RoomQuantity>
    <RoomPerson>2</RoomPerson>
    <IsHoldRoom>F</IsHoldRoom>
    <IsHasRemark>F</IsHasRemark>
    <CheckAvlFirstId />
    <CheckAvlTimes>1</CheckAvlTimes>
    <OperationType>New</OperationType>
    <SearchType>FG_PP</SearchType>
    <CheckAvailType>Query</CheckAvailType>
    <GuestSelectPrice>
      <CheckAvalID>2075157</CheckAvalID>
      <RoomRatesID>1</RoomRatesID>
    </GuestSelectPrice>
    <ChannelSource>Offline</ChannelSource>
    <AvlStatus>1</AvlStatus>
  </GetHotelAvailRequest>
</Request>" ;

        public static void DoRequest()
        {
            new Thread(() =>
            {
                try
                {
                    Console.WriteLine("Thread={0}, Start", Thread.CurrentThread.ManagedThreadId);
                    ServiceReference1.HotelInterfaceSwitchServiceSoapClient client = new ServiceReference1.HotelInterfaceSwitchServiceSoapClient();
                    string Response = client.Request(Request);
                    Interlocked.Increment(ref successNumber);
                    Console.WriteLine("Thread={0}, Success:TotalNumber={1}", Thread.CurrentThread.ManagedThreadId, successNumber);
                    //Console.WriteLine("Thread={0}, Success:XML:{1}", Thread.CurrentThread.ManagedThreadId, Response);

                }
                catch (Exception ex)
                {
                    Interlocked.Increment(ref failNumber);
                    Console.WriteLine("Thread={0}, Ex:{1}", Thread.CurrentThread.ManagedThreadId, ex.Message);
                }

            }).Start();
            
        }
    }
}
